package com.songcha.library_common.ui.dialog

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.MarginLayoutParamsCompat
import androidx.core.view.marginTop
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.songcha.library_common.R
import com.songcha.library_common.ui.adapter.DialogListAdapter
import com.songcha.library_common.util.dp2px


class ListDialog(context: Context,val list:MutableList<String>):CustomDialog(context) {
     fun interface IOnListDialogItemClickListener{
        fun onItemClick(position:Int):Boolean
    }

    init {
        //if list size overflow screen height will cause height error,
        //because container height layout params is wrap_content at this point
        setDialogMsg("")
        setShowBtn(false)
    }

    private var mListener:IOnListDialogItemClickListener?=null

    fun setOnListDialogItemClickListener(listener: IOnListDialogItemClickListener){
        mListener=listener
    }

    override fun hasView(): Boolean {
        return true
    }

    override fun getView(root:ViewGroup): View? {
        val view=LayoutInflater.from(context).inflate(R.layout.common_dialog_list,root,false)
        val rcv=view.findViewById<RecyclerView>(R.id.rcv)
        rcv.apply {
            layoutManager= LinearLayoutManager(context)
            val adt= DialogListAdapter(context,list)
            adapter=adt
            adt.setOnItemChildClickListener{a,v,p->
                when(v.id){
                    R.id.ll_dialog_list->{
                        //if(block(p))
                        mListener?.let {
                            if(it.onItemClick(p)) dismiss()
                        }?: dismiss()

                    }
                }
            }
        }
        return view
    }


}